package br.com.lh.urabuilder.helper
{
	import br.com.lh.urabuilder.business.ViewManager;
	import br.com.lh.urabuilder.event.LinkActionEvent;
	import br.com.lh.urabuilder.view.ActionComponent;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	public class LinkHelper
	{
		private var parentComponent:UIComponent;
		private var viewManager:ViewManager;
		private var mouseEventHelper:MouseEventHelper;
		
		public function LinkHelper(parentComponent:UIComponent,viewManager:ViewManager,mouseEventHelper:MouseEventHelper)	{
			this.parentComponent = parentComponent;
			this.viewManager = viewManager;
			this.mouseEventHelper = mouseEventHelper;
		}
		
		public function initLink(event:DragEvent):void{
        	var dropTarget:ActionComponent = event.currentTarget as ActionComponent;
        	DragManager.acceptDragDrop(dropTarget);
        }
            
        public function finishLink(event:DragEvent):void{
        	if (!mouseEventHelper.isMouseMovingArrow){
        		return;
        	}
        	var arrowContainer:ArrowContainer =ArrowContainer(event.dragInitiator);
        	if (arrowContainer.startComponent == null && arrowContainer.endComponent == null){
        		//NOVA ARROW SENDO INSERIDA.
        	}else{
        		rotateArrow(arrowContainer,event);
        	}
        }
        
        private function rotateArrow(arrowContainer:ArrowContainer,event:DragEvent):void{
        	var componentInWhichArrowIsBeingRotatedTo:ActionComponent = event.currentTarget as ActionComponent;
        	var isEndPoint:Boolean =  Boolean(event.dragSource.dataForFormat('rotateEnd'));
        	var rotateBegin:Boolean =  Boolean(event.dragSource.dataForFormat('rotateBegin'));
        	if (isEndPoint){
        		rotateEndComponent(arrowContainer,event);
        	}else if (rotateBegin){
        		rotateStartComponent(arrowContainer,event);
        	}
        	mouseEventHelper.isMouseMovingArrow= false;
        }
        
        private function rotateStartComponent(arrowContainer:ArrowContainer,event:DragEvent):void{
        	var newStartComponent:ActionComponent = ActionComponent(event.currentTarget);
        	if (newStartComponent.isLinked()){
        		return;
        	}
        	var linkActionEvent:LinkActionEvent = new LinkActionEvent(newStartComponent,arrowContainer.endComponent);
        	viewManager.removeComponent(arrowContainer);
        	CairngormEventDispatcher.getInstance().dispatchEvent(linkActionEvent);
        	
        }
        
        private function rotateEndComponent(arrowContainer:ArrowContainer,event:DragEvent):void{
        	var linkActionEvent:LinkActionEvent = new LinkActionEvent(arrowContainer.startComponent,ActionComponent(event.currentTarget));
        	viewManager.removeComponent(arrowContainer);
        	CairngormEventDispatcher.getInstance().dispatchEvent(linkActionEvent);
        }
        

	}
}